<template>
    <div class="w750 white-bg" :class="themes.theme">
        <!-- 收货地址 -->
        <div class="delivery">
            <van-list
                v-model="paging.loading"
                :finished="paging.finished"
                finished-text="没有更多了"
                @load="onLoad"
                :immediate-check="false"
                v-if="paging.tab"
            >
                <div
                    class="del-address"
                    v-for="(item, index) in addressDto"
                    :key="index"
                    @click="selectAddres(item.addrId, item)"
                    :class="{ on: isSelect ? isSelect == item.addrId : item.commonAddr == 1 }"
                >
                    <em class="choose cur"></em>
                    <div class="address-con">
                        <div class="item-p">
                            <span class="name">{{ item.receiver }}</span>
                            <span class="phone">{{ item.mobile }}</span>
                            <i class="def" v-if="item.commonAddr == 1">默认</i>
							<i class="tag bg-danger" v-if="item.needEdit">请补充街道信息</i>
                        </div>
                        <span class="det">{{ item.province }} {{ item.city }} {{ item.area }} {{ item.town }}</span>
                    </div>
                    <a href="javascript:;" class="edit" @click.stop="goAdd(item.addrId)"><img src="@/assets/images/edit.png" alt="" /></a>
                </div>
            </van-list>
        </div>
        <Noempty :pic="require('@/assets/images/review.png')" :title="'暂无相关地址列表'" v-if="paging.emptylist && !paging.error" />
        <a href="javascript:;" class="delivery-btn" :class="themes.theme" @click.stop="goAdd()">+ 新增地址</a>
        <!-- /收货地址 -->
    </div>
</template>

<script>
import Noempty from 'components/Noempty/Noempty'
import { mapState } from 'vuex'
import { addressList, defaultaddress } from 'api/addressManage'
import PageUtil from 'utils/PageUtil'
const pageUtil = new PageUtil(addressList, {
    field: {
        list: 'addressDto'
    }
})
export default {
    components: {
        Noempty
    },
    data() {
        return {
            isSelect: null,
            addressDto: [], //地址列表
            Nomsg: false,
            paging: {
                tab: false,
                loading: false,
                finished: false,
                error: false, // 是否错误
                emptylist: false // 是否显示列表为空时的样式
            }
        }
    },

    watch: {},
    mounted() {
        let that = this
        this.isSelect = this.$route.query.selectAddres
        this.getAddressDto()
    },
    methods: {
        //选择地址返回售后订单页面
        goEdit() {
            this.$router.back()
            this.$router.isBack = true
        },
        getAddressDto() {
            pageUtil.loadListByPage(this)
        },
        //返回
        goback() {
            this.$router.back()
            this.$router.isBack = true
        },
        //选择地址
        selectAddres(addrId, item) {
            const type = this.$route.query.type

            //如果来源售后详情页
            if (type == 'after') {
                item = JSON.stringify(item)
                sessionStorage.setItem('addressInfo', item)
                this.$router.back()
                this.$router.isBack = true
                return
            }
            //如果来源发票申请页
            if (type == 'invoice') {
                item = JSON.stringify(item)
                sessionStorage.setItem('addressInfo', item)
                this.$router.back()
                this.$router.isBack = true
                return
            }
            addrId = addrId ? addrId : ''
            if (this.$route.query.changeAddress) {
                this.addrId = addrId
                sessionStorage.setItem('addrId', this.addrId)
                // sessionStorage.setItem('changeAddress', true)
                this.$router.back()
                this.$router.isBack = true
                return
            }
        },
        //设置默认地址
        async setDefaultAddress(addrId, index) {
            try {
                let res = await defaultaddress({ addrId: addrId })
                if (res.status != 1) {
                    this.$toast(res.msg)
                    return
                }
                this.addressDto.forEach((addressItem, addressIndex) => {
                    ;(addressIndex === index && (addressItem.commonAddr = '1')) || (addressItem.commonAddr = '0')
                })
                this.addressDto.unshift(this.addressDto.splice(index, 1)[0])
            } catch (error) {
            }
        },
        //去编辑地址
        goAdd(addrId) {
            let query = {
                addrId: addrId
            }

            this.$route.query.changeAddress && (query.changeAddress = this.$route.query.changeAddress)

            this.$router.push({
                path: `/addAddress`,
                query: query
            })

            this.addrId = addrId
            sessionStorage.setItem('addrId', this.addrId)
        },
        //下垃加载请求
        onLoad() {
            pageUtil.loadListByPageMore()
        }
    },
    computed: {
        ...mapState(['themes'])
    }
}
</script>

<style src="../../assets/css/delivery.css" scoped></style>
<style src="../../assets/css/style/my-style.css" scoped></style>
